package com.dinadi.yidi.ui.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.MyCouponsStateRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.ToastUtil;

/**
 * author: JiaYu.He
 * Date: 2017/9/18   兑换码换券
 * Time: 15:32
 */
public class ExChangeActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private ImageView btn_ex_change;
    private EditText edit_redeem_code;
    private String redeemCode, token, userId;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(ExChangeActivity.this).inflate(R.layout.activity_ex_change, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {

        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_my_exchange));
        btn_ex_change = (ImageView) view.findViewById(R.id.btn_ex_change);
        edit_redeem_code = (EditText) view.findViewById(R.id.edit_redeem_code);
    }

    @Override
    protected void initListener() {
        registerClickListener(btn_ex_change);

    }

    @Override
    public void onResume() {
        super.onResume();
        String isRefresh = SharePreferencesHelper.getInstance(ExChangeActivity.this).getString("isRefresh");
        if ("yes".equals(isRefresh)) {
            userId = SharePreferencesHelper.getInstance(ExChangeActivity.this).getString("userId");
            token = SharePreferencesHelper.getInstance(ExChangeActivity.this).getString("token");
            SharePreferencesHelper.getInstance(ExChangeActivity.this).putString("isRefresh", "no");
        }
    }

    @Override
    protected void initData() {
        token = SharePreferencesHelper.getInstance(this).getString("token");
        userId = SharePreferencesHelper.getInstance(this).getString("userId");

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_ex_change:
                redeemCode = edit_redeem_code.getText().toString();
                if (redeemCode.isEmpty()) {
                    ToastUtil.showToast("请输入兑换码");
                } else {
                    exchange();
                }

                break;
            default:
                break;
        }

    }

    private void exchange() {

        MyCouponsStateRequestEntity bean = new MyCouponsStateRequestEntity();
        bean.setUserId(userId);
        bean.setToken(token);
        bean.setCouponCode(redeemCode);
        CommonInterface commonInterface = new CommonInterface(ExChangeActivity.this, httpListener);
        commonInterface.getInterface("16023", JSON.toJSONString(bean));

    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                edit_redeem_code.setText("");
                ToastUtil.showToast("兑换成功");
                SharePreferencesHelper.getInstance(ExChangeActivity.this).putString("isRefreshCoupon","is");

            } else {
                ToastUtil.showToast(errMsg);

            }
        }

        @Override
        public void OnFail(String errorResponse) {
            ToastUtil.showToast("兑换失败");
            super.OnFail(errorResponse);
        }
    };
}
